/**
 * 
 */
package com.javaking.clanteam.studentutils.courses;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.javaking.clanteam.studentutils.R;
import com.javaking.clanteam.studentutils.courses.assingments.AssignmentData;
import com.javaking.clanteam.studentutils.sql.CourseHelper;
import com.javaking.clanteam.studentutils.utils.DatePair;
import com.javaking.clanteam.studentutils.utils.EditableListFragment;
import com.javaking.clanteam.studentutils.utils.OnItemsChangedListener;
import com.javaking.clanteam.studentutils.utils.TabsAdapter;

/**
 * @author scott
 *
 */
public class CourseManagementActivity extends SherlockFragmentActivity 
		implements	OnItemsChangedListener<DatePair>{
	
	CourseData mCourse;
	
	private EditableListFragment<DatePair> mTimesFrag;
	private EditableListFragment<AssignmentData> mAssignmentFrag;
	private TabsAdapter mTabs;
	private ViewPager mViewPager;
	private ActionBar mActionBar;
	
	/**
	 * @see android.app.Fragment#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mCourse = getIntent().getParcelableExtra("course");
		
		setContentView(R.layout.fragment_coursemanagement);
		
		mViewPager.setOnPageChangeListener(
            new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When swiping between pages, select the
                    // corresponding tab.
                    getSupportActionBar().setSelectedNavigationItem(position);
                }
            });
		
		mActionBar = getSupportActionBar();
		
		mActionBar.newTab().setTabListener(new ActionBar.TabListener() {
			
			@Override
			public void onTabUnselected(Tab tab, FragmentTransaction ft) {	}
			
			@Override
			public void onTabSelected(Tab tab, FragmentTransaction ft) {
				mViewPager.setCurrentItem(tab.getPosition());
			}
			
			@Override
			public void onTabReselected(Tab tab, FragmentTransaction ft) {}
		});
		
		mTabs = null;
		
		List<Fragment> tabs = new ArrayList<Fragment>();
		mTimesFrag = EditableListFragment.createInstance(this, mCourse.getTimes(), false, null,false);
		tabs.add(mTimesFrag);
		mAssignmentFrag = EditableListFragment.createInstance(this, null, false, null,false);
		tabs.add(mAssignmentFrag);
		
		mTabs.addTabs(tabs);
		
		mViewPager.setAdapter(mTabs);
//		
//		getSupportFragmentManager().beginTransaction()
//			.replace(R.id.frame, mTabs,mTabs.getClass().getName())
//			.commit();
		
		updateData();
	}
	
//	/**
//	 * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
//	 */
//	@Override
//	public void onCreate(Bundle a) {
//		View view = inflater.inflate(R.layout.fragment_coursemanagement, container, false);
//		
//		mTabs = (TabFragment) Fragment.instantiate(getActivity(), TabFragment.class.getName());
//		
//		List<Fragment> tabs = new ArrayList<Fragment>();
//		mTimesFrag = EditableListFragment.createInstance(this, mCourse.getTimes(), false, null,false);
//		tabs.add(mTimesFrag);
//		mAssignmentFrag = EditableListFragment.createInstance(this, null, false, null,false);
//		tabs.add(mAssignmentFrag);
//		
//		mTabs.addTabs(tabs);
//		
//		getChildFragmentManager().beginTransaction()
//			.replace(R.id.frame, mTabs,mTabs.getClass().getName())
//			.commit();
//		
//		updateData(view);
//		
//		return view;
//	}
	
//	@Override
//	public void onActivityCreated(Bundle savedInstanceState) {
//		super.onActivityCreated(savedInstanceState);
//		mTimesFrag = EditableListFragment.addToFrame(this,
//				mCourse!=null ? mCourse.getTimes() : null,
//				false, NewTimeDialog.class);
//	}
	
	@SuppressLint("NewApi")
	@Override
	public void onResume() {
		super.onResume();
		updateData();
		getSupportActionBar().setTitle(String.format("Manage %s",mCourse.getCourseTitle()));
	}

	public void updateData() {
		CourseHelper helper = new CourseHelper(this);
		mCourse = helper.getCourseById(mCourse.getId());
		if(mCourse==null) {
			finish();
		}
		
		((TextView)findViewById(R.id.courseTitle)).setText(mCourse.getCourseTitle());
		((TextView)findViewById(R.id.teacher)).setText(mCourse.getTeacher());
		((TextView)findViewById(R.id.roomNumber)).setText(mCourse.getRoom());
//		if (mTimesFrag!=null) {
//			mTimesFrag.updateData(mCourse.getTimes());
//		}
	}
	
	
//	@Override
//	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//		inflater.inflate(R.menu.fragment_coursemanagement, menu);
//	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.fragment_coursemanagement, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Fragment frag = null;
		Bundle args = new Bundle();
		switch (item.getItemId()) {
		case R.id.editCourse:
			frag = new EditCourseFragment();
			args.putParcelable("course", mCourse);
			break;
		case R.id.removeCourse:
			frag = new RemoveCourseFragment();
			args.putParcelable("course", mCourse);
			break;
		default:
			return super.onOptionsItemSelected(item);
		}
		
		frag.setArguments(args);
		
		if (frag instanceof DialogFragment) {
			((DialogFragment)frag).show(getSupportFragmentManager(), null);
		} else {		
			getSupportFragmentManager().beginTransaction().replace(android.R.id.content, frag)
				.addToBackStack(null).commit();
		}
		return true;

	}

	@Override
	public void onItemsAdded(List<DatePair> items) {
		
	}

	@Override
	public void onItemRemoved(DatePair item) {
		// TODO Auto-generated method stub
		
	}
	
}
